package com.lamda.zhekou.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MJiao on 15/8/14.
 */
public class SMSUtil {

    public static final String ACCOUNT = "cf_jxscwl";

    public static final String PASSWORD = "Aho123456A";

    public static boolean sendSMS(long phoneNum, int volidateCode) {
        String content = new String("您的验证码是：【变量】。请不要把验证码泄露给其他人。").replace("【变量】", String.valueOf(volidateCode));
        return SMSUtil.sendSMS(phoneNum, content);
    }

    public static boolean sendSMS(long phoneNum, String content) {

        try {
            content = URLEncoder.encode(content,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit&account=" + ACCOUNT + "&password=" + PASSWORD + "&mobile=" + phoneNum + "&content=您的验证码是：" + content + "。请不要把验证码泄露给其他人。";


        try {
            String result = HttpUtils.get(url, null);
            if (StringUtils.isNotEmpty(result) && result.contains("提交成功")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }


    public static boolean sendSevenExpireWarning(long phoneNum, Timestamp expireTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = simpleDateFormat.format(expireTime);
        String template = "尊敬的用户：您有云服务器VPS将于【变量】正式到期，截至目前仅剩7天。如您要继续使用，请于到期前续费。";
        String content = new String(template).replace("【变量】", String.valueOf(endTime));
        return SMSUtil.sendSMS(phoneNum, content);
    }

    public static boolean sendThreeDayExpireWarning(long phoneNum, Timestamp expireTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = simpleDateFormat.format(expireTime);
        String template = "尊敬的用户：您有云服务器VPS将于【变量】正式到期，截至目前仅剩3天。如您要继续使用，请于到期前续费。";
        String content = new String(template).replace("【变量】", String.valueOf(endTime));

        return SMSUtil.sendSMS(phoneNum, content);
    }

    public static boolean sendLessOneDayExpireWarning(long phoneNum) {
        String content = "尊敬的用户：您有云服务器VPS将于今天正式到期，如您要继续使用，请及时续费。";
        return SMSUtil.sendSMS(phoneNum, content);
    }

    public static void main(String[] args) {
        sendThreeDayExpireWarning(15180810240l, new Timestamp(System.currentTimeMillis()));
    }
}
